package com.qyer.search.model.mutable;

import com.qyer.search.cache.mutable.MutableCaches;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;

import java.nio.channels.MulticastChannel;
import java.util.*;
/**
 * Created by tangzheng on 16/9/5.
 */
public class MarketingInfoForPlace {
  private Map<Integer, MarketingListInfo> cityMarketInfo;
  private Map<Integer, MarketingListInfo> countryMarketInfo;

  public MarketingInfoForPlace() {
    cityMarketInfo = new HashMap<>();
    countryMarketInfo = new HashMap<>();
  }

  public Map<Integer, MarketingListInfo> getCityMarketInfo() {
    return cityMarketInfo;
  }

  public void setCityMarketInfo(Map<Integer, MarketingListInfo> cityMarketInfo) {
    this.cityMarketInfo = cityMarketInfo;
  }

  public Map<Integer, MarketingListInfo> getCountryMarketInfo() {
    return countryMarketInfo;
  }

  public void setCountryMarketInfo(Map<Integer, MarketingListInfo> countryMarketInfo) {
    this.countryMarketInfo = countryMarketInfo;
  }

  public void addNewMarketI(int place_id, int type, String[] strs, long start, long end){
    int ll = strs.length;
    if(ll > 0){

      MarketingListInfo marketingListInfo = new MarketingListInfo(ll);
      marketingListInfo.addMI(strs);
      marketingListInfo.setStart(start);
      marketingListInfo.setEnd(end);
      marketingListInfo.setPlaceId(place_id);
      marketingListInfo.setType(type);

      if(type == 1){
        countryMarketInfo.put(place_id, marketingListInfo);
      }else if(type == 2){
        cityMarketInfo.put(place_id, marketingListInfo);
      }
    }
  }

  @Override public boolean equals(Object o) {
    if (this == o)
      return true;
    if (o == null || getClass() != o.getClass())
      return false;

    MarketingInfoForPlace that = (MarketingInfoForPlace) o;

    if (!cityMarketInfo.equals(that.cityMarketInfo))
      return false;
    return countryMarketInfo.equals(that.countryMarketInfo);

  }

  @Override public int hashCode() {
    int result = cityMarketInfo.hashCode();
    result = 31 * result + countryMarketInfo.hashCode();
    return result;
  }

  @Override public String toString() {
    return "MarketingInfoForPlace{" +
      "cityMarketInfo=" + cityMarketInfo +
      ", countryMarketInfo=" + countryMarketInfo +
      '}';
  }
  //返回结果null标志不匹配
  public MarketingListInfo getFromUserPrefer(PreferDestination preferDestination){
    long tt = System.currentTimeMillis();
    if(preferDestination == null)
      return null;
    if(preferDestination.isEmpty())
      return null;
    Map<Integer, Double> cm1 = preferDestination.getCityPrefer();
    Map<Integer, Double> cm2 = preferDestination.getCoutryPrefer();

    double weight = 0;
    MarketingListInfo marketingListInfo = null;
    MarketingListInfo marketingListInfo1 = null;
    long t = System.currentTimeMillis();
    if(MapUtils.isNotEmpty(cm1) && MapUtils.isNotEmpty(this.cityMarketInfo)){
      System.out.println("XA");
      Set<Integer> s1 = cm1.keySet();
      for(Integer integer:s1){
        if(cityMarketInfo.containsKey(integer)){
          double weight1 = cm1.get(integer);
          if(weight1 > weight) {
            marketingListInfo1 = cityMarketInfo.get(integer);
            if(marketingListInfo1.isValid(t)) {
              marketingListInfo = marketingListInfo1;
              weight = weight1;
            }
          }
        }
      }
    }

    if(MapUtils.isNotEmpty(cm2) && MapUtils.isNotEmpty(this.countryMarketInfo)){
      System.out.println("XB");
      Set<Integer> s1 = cm2.keySet();
      for(Integer integer:s1){
        if(countryMarketInfo.containsKey(integer)){
          double weight1 = cm2.get(integer);
          if(weight1 > weight) {
            marketingListInfo1 = countryMarketInfo.get(integer);
            if(marketingListInfo1.isValid(t)) {
              marketingListInfo = marketingListInfo1;
              weight = weight1;
            }
          }
        }
      }
    }

    long t1 = System.currentTimeMillis();
    System.out.println("getFromUserPrefer:"+(t1-tt));
    return marketingListInfo;
  }

  public boolean hasThisCity(int cid){
    long t = System.currentTimeMillis();
    if(cityMarketInfo != null){
      if(cityMarketInfo.containsKey(cid)){
        MarketingListInfo marketingListInfo = cityMarketInfo.get(cid);
        if(marketingListInfo.isValid(t))
          return true;
      }
    }
    //if(countryMarketInfo)

    return false;
  }

  public boolean isEmpty(){
    if(MapUtils.isEmpty(this.cityMarketInfo) && MapUtils.isEmpty(this.countryMarketInfo))
      return true;
    return false;
  }

  ///////////////////////////////////
  public MarketingListInfo getCityMarketListInfo(int cityId){
    long t = System.currentTimeMillis();
    int countryId = -1;
    if(MutableCaches.getInstance().CITY_COUNTRY_ID_MAP.getCacheContent().containsKey(cityId))
      countryId = MutableCaches.getInstance().CITY_COUNTRY_ID_MAP.getCacheContent().get(cityId);
    MarketingListInfo marketingListInfo = new MarketingListInfo(20);
    if (this.cityMarketInfo.containsKey(cityId)){
      MarketingListInfo marketingListInfo1 = cityMarketInfo.get(cityId);
      if(marketingListInfo1.isValid(t)){
        marketingListInfo.addMarketInfo(marketingListInfo1);
      }
    }
    if(this.countryMarketInfo.containsKey(countryId)){
      MarketingListInfo marketingListInfo1 = countryMarketInfo.get(countryId);
      if(marketingListInfo1.isValid(t)){
        marketingListInfo.addMarketInfo(marketingListInfo1);
      }
    }
    return marketingListInfo;
  }
  ///////////////////无个性化////////////////
  public MarketingListInfo getCountryMLInfo(int countryId){
    long t = System.currentTimeMillis();
    Map<Integer, Integer> ccMapInfo = MutableCaches.getInstance().CITY_COUNTRY_ID_MAP.getCacheContent();
    MarketingListInfo marketingListInfo = new MarketingListInfo(20);
    if(this.countryMarketInfo.containsKey(countryId)){
      MarketingListInfo marketingListInfo1 = countryMarketInfo.get(countryId);
      if(marketingListInfo1.isValid(t)){
        marketingListInfo.addMarketInfo(marketingListInfo1);
      }
    }
    if(MapUtils.isNotEmpty(this.cityMarketInfo)){
      Set<Integer> set = cityMarketInfo.keySet();
      for(int i:set){
        if(ccMapInfo.containsKey(i))
          if(countryId == (ccMapInfo.get(i))){
            MarketingListInfo marketingListInfo1 = cityMarketInfo.get(i);
            if(marketingListInfo1.isValid(t)){
              marketingListInfo.addMarketInfo(marketingListInfo1);
            }
          }
      }
    }

    return marketingListInfo;
  }

  ////////////////////////////////////////////
  public MarketingListInfo getContinentMLInfo(int continentId){
    long t = System.currentTimeMillis();
    Map<Integer, Integer> ccMapInfo = MutableCaches.getInstance().COUNTRY_CONTINENT_ID_MAP.getCacheContent();
    MarketingListInfo marketingListInfo = new MarketingListInfo(20);
    if(MapUtils.isNotEmpty(countryMarketInfo)){
      Set<Integer> set = countryMarketInfo.keySet();
      for(int i:set){
        if(ccMapInfo.containsKey(i))
          if(continentId == (ccMapInfo.get(i))){
            MarketingListInfo marketingListInfo1 = cityMarketInfo.get(i);
            if(marketingListInfo1.isValid(t)){
              marketingListInfo.addMarketInfo(marketingListInfo1);
            }
          }
      }
    }

    return marketingListInfo;
  }

  ////////////////////////////////////////////////////////////
  public MarketingListInfo getMLInfoWithUserPrefer(PreferDestination preferDestination){
    long t = System.currentTimeMillis();
    if(preferDestination == null)
      return null;
    if(preferDestination.isEmpty())
      return null;
    Map<Integer, Double> cm1 = preferDestination.getCityPrefer();
    Map<Integer, Double> cm2 = preferDestination.getCoutryPrefer();
    MarketingListInfo marketingListInfo1 = new MarketingListInfo(20);
    MarketingListInfo marketingListInfo2 = new MarketingListInfo(20);
    MarketingListInfo marketingListInfo3 = new MarketingListInfo(20);
    MarketingListInfo marketingListInfo = null;
    double weight = 0;
    if(MapUtils.isNotEmpty(cityMarketInfo) && MapUtils.isNotEmpty(cm1)) {
      Iterator<Map.Entry<Integer, Double>> iterator = cm1.entrySet().iterator();
      while (iterator.hasNext()) {
        Map.Entry<Integer, Double> entry = iterator.next();
        int key = entry.getKey();
        if(cityMarketInfo.containsKey(key)){
          weight = entry.getValue();
          if(weight > 100){
            marketingListInfo = cityMarketInfo.get(key);
            if(marketingListInfo.isValid(t))
              marketingListInfo1.addMarketInfo(cityMarketInfo.get(key));
          }else if(weight > 50){
            marketingListInfo = cityMarketInfo.get(key);
            if(marketingListInfo.isValid(t))
              marketingListInfo2.addMarketInfo(cityMarketInfo.get(key));
          }else {
            marketingListInfo = cityMarketInfo.get(key);
            if(marketingListInfo.isValid(t))
              marketingListInfo3.addMarketInfo(cityMarketInfo.get(key));
          }
        }
      }
    }

    if(MapUtils.isNotEmpty(countryMarketInfo) && MapUtils.isNotEmpty(cm2)) {
      Iterator<Map.Entry<Integer, Double>> iterator = cm2.entrySet().iterator();
      while (iterator.hasNext()) {
        Map.Entry<Integer, Double> entry = iterator.next();
        int key = entry.getKey();
        if(countryMarketInfo.containsKey(key)){
          weight = entry.getValue();
          if(weight > 100){
            marketingListInfo1.addMarketInfo(this.getCountryMLInfo(key));
            /*
            marketingListInfo = countryMarketInfo.get(key);
            if(marketingListInfo.isValid(t))
              marketingListInfo1.addMarketInfo(countryMarketInfo.get(key));
              */
          }else if(weight > 50){
            marketingListInfo2.addMarketInfo(this.getCountryMLInfo(key));
            /*
            marketingListInfo = countryMarketInfo.get(key);
            if(marketingListInfo.isValid(t))
              marketingListInfo2.addMarketInfo(countryMarketInfo.get(key));
            */
          }else {
            marketingListInfo3.addMarketInfo(this.getCountryMLInfo(key));
            /*
            marketingListInfo = countryMarketInfo.get(key);
            if(marketingListInfo.isValid(t))
              marketingListInfo3.addMarketInfo(countryMarketInfo.get(key));
            */
          }
        }
      }
    }
    marketingListInfo1.addMarketInfo(marketingListInfo2);
    marketingListInfo1.addMarketInfo(marketingListInfo3);
    System.out.println("oo:OO"+(System.currentTimeMillis() - t));
    return marketingListInfo1;
  }
  //////////////////////////////////////////////////////////////
}
